﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.RemoteCtrls.Custom;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DWF.Studio.RemoteCtrls.ActivityToolBox
{
    /// <summary>
    /// SearchControl.xaml 的交互逻辑
    /// </summary>
    public partial class UCtrlSearchBox : UserControl
    {
        public UCtrlSearchBox()
        {
            InitializeComponent();
        }

        //搜索事件-传递到外层
        public event EventHandler<SearchEventArgs> OnSearch;

        /// <summary>
        /// 搜索操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSearch_OnClick(object sender, RoutedEventArgs e)
        {
            if (null == TbxInput)
                return;

            //ExeccuteSearch(TbxInput.Text);
        }

        /// <summary>
        /// 搜索操作
        /// </summary>
        /// <param name="strSearchText"></param>
        private void ExeccuteSearch(String strSearchText)
        {
            if (OnSearch != null)
            {
                var args = new SearchEventArgs();
                args.SearchText = strSearchText;
                OnSearch(this, args);
            }
        }

        /// <summary>
        /// 清除输入内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnClearInput_OnClick(object sender, RoutedEventArgs e)
        {
            TbxInput.Text = "";
            ExeccuteSearch("");
        }

        private void Uc_TbxContent_TextChanged(object sender, TextChangedEventArgs e)
        {
            String strSearchText = (sender as TextBox).Text;
            TbxInput.Text = strSearchText;
            ExeccuteSearch(strSearchText);
        }

        private void clearSearchBtn_Click(object sender, RoutedEventArgs e)
        {
            TextBox txtSearchBox = WPFExtensionMethods.FindVisualChild<TextBox>(FindName("TbxInput") as DependencyObject);
            txtSearchBox.Text = string.Empty;
        }
    }

    /// <summary>
    /// 搜索事件定义
    /// </summary>
    public class SearchEventArgs : EventArgs
    {
        public string SearchText { get; set; }
    }
}
